package com.taropan.wallpaper.ui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.taropan.wallpaper.R;
import com.taropan.wallpaper.model.HomepageInfo;
import com.taropan.wallpaper.model.LastUpdatedWallpaper;
import com.taropan.wallpaper.model.PugListAdapter;
import com.taropan.wallpaper.model.TodayRecommend;
import com.taropan.wallpaper.util.Consts;
import com.taropan.wallpaper.util.HttpUtils;
import com.taropan.wallpaper.util.UICore;
import com.taropan.wallpaper.util.Utils;
import com.taropan.wallpaper.widget.CustomGridView;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.widget.GridView;
import android.widget.Switch;

public class MainPageActivity extends BaseActivity implements Callback{

private static final String TAG = MainPageActivity.class.getSimpleName();

private static final int EXEU_GET_HOMEPAGE_INFO = 0;

	private CustomGridView mGridView;
	private Handler handler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Utils.dout(TAG, "onCreate");
		setContentView(R.layout.tab_mainpage);
		handler = new Handler(this);
		mGridView = (CustomGridView) findViewById(R.id.gridView1);
		mGridView.setHaveScrollbar(false);
		initHead();
//		btn_leftTop.setVisibility(View.INVISIBLE);
		tv_head.setText("MainPageActivity");
		tv_head.setVisibility(View.INVISIBLE);
		
		UICore.eventTask(this, MainPageActivity.this, EXEU_GET_HOMEPAGE_INFO, "", null);
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Utils.dout(TAG, "onResume");
	}
	
	
	private void getHomepagelist() {
		StringBuffer url = new StringBuffer(Consts.API_HOMEPAGE);
		Utils.dout("getHomepagelist url: " + url.toString());
		try {
			String results = HttpUtils.doGet(url.toString());

			if (results == null) {
				return;
			}
			Utils.dout("getHomepagelist results:" + results.toString());
			Gson gson = new Gson();
			HomepageInfo bottleInfo = gson.fromJson(results, HomepageInfo.class);
			List<LastUpdatedWallpaper> todayRecommends = bottleInfo.getLastUpdatedWallpaper();
			Message localMessage = Message.obtain(handler, EXEU_GET_HOMEPAGE_INFO, todayRecommends);
			handler.sendMessage(localMessage);	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
		public void execute(int mes, Object obj) {
			super.execute(mes, obj);
			switch (mes) {
			case EXEU_GET_HOMEPAGE_INFO:
				getHomepagelist();
				break;

			default:
				break;
			}
		}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case EXEU_GET_HOMEPAGE_INFO:
			List<LastUpdatedWallpaper> todayRecommends = (List<LastUpdatedWallpaper>) msg.obj;

			if (!todayRecommends.isEmpty()) {
				PugListAdapter adapter = new PugListAdapter(MainPageActivity.this, todayRecommends);
				mGridView.setAdapter(adapter);	
			}
			
			break;

		default:
			break;
		}
		return false;
	}
	
	
}
